using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace demo001.Models
{

    public class VideoDto
    {
        public long VideoId { get; set; }
        public string Title { get; set; }
        public int Duration { get; set; }
        public string VideoUrl { get; set; }
        public long? FileSize { get; set; } // 修改为可空类型，因为API返回null
        public DateTime CreatedDate { get; set; }
        public DateTime UpdatedDate { get; set; }
    }
    //"{\"items\":[{\"videoId\":1,\"title\":\"24式太极拳 _ 慢速、带呼吸导引、跟练版来了！\",\"duration\":528,\"videoUrl\":\"E:\\\\工作\\\\中联软件相关文件\\\\java\\\\项目代码\\\\AI写程序007\\\\聊天机器人+培训\\\\测试视频\\\\24式太极拳 _ 慢速、带呼吸导引、跟练版来了！.mp4\",\"fileSize\":null,\"createdDate\":\"2025-04-10T21:36:29.397\",\"updatedDate\":\"2025-04-11T11:12:22.055\"}],\"totalCount\":1,\"pageNumber\":1,\"pageSize\":10,\"totalPages\":1,\"hasPreviousPage\":false,\"hasNextPage\":false}"
    /// <summary>
    /// 视频数据传输对象
    /// </summary>
    public class VideoViewModel : VideoDto
    {
        private long _videoId;
        private string _title = string.Empty;
        private int _duration;
        private string _videoUrl = string.Empty;
        private long _fileSize;
        private DateTime _createdDate;
        private DateTime _updatedDate;
        private bool _isSubscribed;
        private string _localFilePath = string.Empty;
        private double _downloadProgress;
        private bool _isDownloading;

        /// <summary>
        /// 视频ID
        /// </summary>
        public long VideoId
        {
            get => _videoId;
            set
            {
                if (_videoId != value)
                {
                    _videoId = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 视频标题
        /// </summary>
        public string Title
        {
            get => _title;
            set
            {
                if (_title != value)
                {
                    _title = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 视频时长（秒）
        /// </summary>
        public int Duration
        {
            get => _duration;
            set
            {
                if (_duration != value)
                {
                    _duration = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(DurationFormatted));
                }
            }
        }

        /// <summary>
        /// 格式化的视频时长（如 "01:30:45"）
        /// </summary>
        public string DurationFormatted
        {
            get
            {
                TimeSpan time = TimeSpan.FromSeconds(Duration);
                return time.Hours > 0 
                    ? $"{time.Hours:00}:{time.Minutes:00}:{time.Seconds:00}" 
                    : $"{time.Minutes:00}:{time.Seconds:00}";
            }
        }

        /// <summary>
        /// 视频URL
        /// </summary>
        public string VideoUrl
        {
            get => _videoUrl;
            set
            {
                if (_videoUrl != value)
                {
                    _videoUrl = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 文件大小（字节）
        /// </summary>
        public long FileSize
        {
            get => _fileSize;
            set
            {
                if (_fileSize != value)
                {
                    _fileSize = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(FileSizeFormatted));
                }
            }
        }

        /// <summary>
        /// 格式化的文件大小（如 "10.5 MB"）
        /// </summary>
        public string FileSizeFormatted
        {
            get
            {
                if (FileSize < 1024)
                    return $"{FileSize} B";
                else if (FileSize < 1024 * 1024)
                    return $"{FileSize / 1024.0:F1} KB";
                else if (FileSize < 1024 * 1024 * 1024)
                    return $"{FileSize / (1024.0 * 1024.0):F1} MB";
                else
                    return $"{FileSize / (1024.0 * 1024.0 * 1024.0):F1} GB";
            }
        }

        /// <summary>
        /// 创建日期
        /// </summary>
        public DateTime CreatedDate
        {
            get => _createdDate;
            set
            {
                if (_createdDate != value)
                {
                    _createdDate = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 更新日期
        /// </summary>
        public DateTime UpdatedDate
        {
            get => _updatedDate;
            set
            {
                if (_updatedDate != value)
                {
                    _updatedDate = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 是否已订阅
        /// </summary>
        public bool IsSubscribed
        {
            get => _isSubscribed;
            set
            {
                if (_isSubscribed != value)
                {
                    _isSubscribed = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(SubscriptionText));
                }
            }
        }

        /// <summary>
        /// 订阅按钮文本
        /// </summary>
        public string SubscriptionText => IsSubscribed ? "已订阅" : "订阅";

        /// <summary>
        /// 本地文件路径
        /// </summary>
        public string LocalFilePath
        {
            get => _localFilePath;
            set
            {
                if (_localFilePath != value)
                {
                    _localFilePath = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 下载进度（0-100）
        /// </summary>
        public double DownloadProgress
        {
            get => _downloadProgress;
            set
            {
                if (_downloadProgress != value)
                {
                    _downloadProgress = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 是否正在下载
        /// </summary>
        public bool IsDownloading
        {
            get => _isDownloading;
            set
            {
                if (_isDownloading != value)
                {
                    _isDownloading = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 属性变更事件
        /// </summary>
        public event PropertyChangedEventHandler? PropertyChanged;

        /// <summary>
        /// 触发属性变更通知
        /// </summary>
        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}